package entity.lemming;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.*;
import entity.terrain.Terrain;
import level.Level;

/**
 * A Bomber Lemming, a Lemming that self destructs after 5 seconds.
 * 
 * @author Neil Cowzer
 */
public class Bomber extends Lemming {
	
	
	
	/** countdown till self-destruct */
	private int timer = 100;
	
	/** the current count */
	private int count;
	

	
	/**
	 * Create an instance of the Bomber Lemming.
	 * 
	 * @param x the starting x-coordinate.
	 * @param y the starting y-coordinate.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X != 0 && energy == 100;
	 */
	public Bomber(int x, int y, String lasttype){
		super("resource/graphics/lemming/walker/walker1.gif",9, x, y);
		speed_X = 1;
		speed_Y = 0;
		energy = 100;
		count = 0;
		this.lasttype = lasttype;
	}
	
	/**
	 * Create a new Bomber with the given center point
	 * 
	 * @param center point of the Lemming
	 */
	public Bomber(Point center, String lasttype){
		super("resource/graphics/lemming/walker/walker1.gif",9, center);
		speed_X = 1;
		speed_Y = 0;
		energy = 100;
		count = 0;
		this.lasttype = lasttype;
		
		if (lasttype == "Blocker"){
			generateFrames("resource/graphics/lemming/blocker/blocker_1.gif", 15);
		}
	}
	
	/**
	 * Check to see if a Lemming collides with another entity
	 * 
	 * @param other another lemming.
	 * @return true if collision occurs.
	 */
	public /*@ pure @*/ boolean collision(Lemming other){
		// get my size
		Rectangle me = getBounds();
		// if we hit a blocker
		if (other instanceof Blocker || (other instanceof Bomber && other.getLastType().equalsIgnoreCase("Blocker"))){
			if (me.intersects(other.getBounds())){
				//turnAround();
				return true;
			}
		}
				return false;
	}
	
	/** Request that this lemming perform the appropiate action/movement. */
	/*@ ensures x == \old(x) + speed_X &&
				energy == \old(energy) - 1;
	 */
	public void move(Terrain terrain) {
		int new_y = y;
		Rectangle lemming = getBounds();
		
		
		if (count == timer -28){
			// only display the shaking head animation, no movement
			generateFrames("resource/graphics/lemming/exploder/exploder_1.gif", 32);
			speed_X = 0;
		}
		else if (timer == count){
			terrain.destroyTerrain(x, y+lemming.height, "bomber");
			alive = false;
		}
		else {
			if (lasttype == "Walker"){
			if (lemming.intersects(terrain.getBounds())){
				BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
					int black = -16777216; // black that represents the background
					int black2 = -16776960;
					
					try{
						
						for (int curr_y = lemming.y; curr_y < lemming.y + 2*lemming.height; curr_y++){
							int color = 0;
							
							
								if (speed_X > 0){
									color = image.getRGB(x + lemming.width/2, curr_y);
								}
								else {
									color = image.getRGB(x + lemming.width/4, curr_y);
								}
							
								if (color != black){
									if(color != black2){
										if(curr_y <= lemming.y + lemming.height/2){
											turnAround();
										}
										else{
											new_y = curr_y - lemming.height -1;
										}
										break;
									}
								}
								else{
									// Gaurd! not allowed fall unless all the "ground" beneath him is black
									boolean onlyBlack = true;
									while (onlyBlack){
										for (int fx = lemming.x; fx<lemming.x+lemming.width; fx++){
											for (int fy = lemming.y+lemming.height; fy <lemming.y+lemming.height+lemming.height/2; fy++){
												int testcolor =  image.getRGB(fx, fy);
												//System.out.println(testcolor);
												if (testcolor != black){
													if(testcolor != black2){
													onlyBlack = false;
													}
													break;
												}
											}
										}
										//break;
									}
									if (onlyBlack){
										fall = true;
									}
									
								}
						}
					}catch(ArrayIndexOutOfBoundsException e){
						
					}
			}
			}
			else {
				speed_X = 0;
			}
		}	
			// Simple check!
			x= x+speed_X;
			y = new_y;
			count++;
			if (timer-1 >= count){
				update();
			}
			
			
		

	}
	
	/** Reverse a Lemmings direction */
	//@ ensures speed_Y == 0 - \old(speed_Y);
	public void turnAround(){
		speed_X = -(speed_X);
		if (lasttype == "Walker"){
			
			if (speed_X > 0){
				generateFrames("resource/graphics/lemming/walker/walker1.gif", 8);
			}
			else {
				generateFrames("resource/graphics/lemming/walker/walkerLeft_1.gif", 8);
			}
		}
		// Blocker doesn't need to turn around
		
	}

}
